/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*!
  @file LayerItem.cpp
  @author Darryl Wallace
  @date 2011
  @brief This file defines the layer item.  A layer item is a QGraphicsItem that holds the image for drawing.
  */

#include "LayerItem.h"

LayerItem::LayerItem(QGraphicsRectItem *parent)
    : QGraphicsItem((QGraphicsItem *)parent),
      _parent(parent)
{
    if (parent)
        _layerImage = QImage(_parent->rect().width(),parent->rect().height(), QImage::Format_ARGB32);
    else
        _layerImage = QImage(1,1, QImage::Format_ARGB32);
}

QRectF LayerItem::boundingRect() const
{
    QSize s = _layerImage.size();

    return QRectF(0.0, 0.0, (qreal)s.width(), (qreal)s.height());
}

const QImage & LayerItem::getImage() const
{
    return _layerImage;
}

QImage * LayerItem::getImagePtr()
{
    return &_layerImage;
}

void LayerItem::setImage(const QImage &image)
{
    _layerImage=image;
    _parent->scene()->invalidate();
}


void LayerItem::resize(int width, int height, ResizeAnchor anchorLocation)
{
    QSize old_size=_layerImage.size();
    QImage old_image;
    if (width>old_size.width() && height>old_size.height())
        old_image=_layerImage;
    else
    {
//        switch (anchorLocation);
    }


    _layerImage=QImage(width, height, QImage::Format_ARGB32);
    _layerImage.fill(0x00000000);   //0xAARRBBGG

    QSize old_pos;
    switch(anchorLocation)
    {
    case TopLeft:
        old_pos=QSize(0,0);
        break;
    default:
        old_pos=QSize(0,0);
        break;
    }

}

void LayerItem::scaleImage(qreal sx, qreal sy)
{

}

void LayerItem::fillImage(const QColor &color)
{
    QPainter p(&_layerImage);
    p.fillRect(0,0,_layerImage.width(),_layerImage.height(),color);
}

void LayerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
    painter->drawImage(0,0,_layerImage);
}

void LayerItem::addItem(QGraphicsItem *item)
{
    QPainter p(&_layerImage);
    QStyleOptionGraphicsItem styleoption;
    item->paint(&p, &styleoption);
}


